

library(stargazer)
library(cjoint)
library(nnet)
library(MNLpred)
library(lfe)

get_ses <- function(model, type) {
    sqrt(diag(sandwich::vcovHC(model, type = type)))
}

# Information experiment

survey <- read.csv("data/qualtrics/brazil/brazil_clean.csv")

survey[survey == ""] <- NA

survey$treatment <- relevel(as.factor(survey$treatment), "high")

survey <- survey[complete.cases(survey[, c(
    "race",
    "treatment", "gender_q",
    "industry_q", "education_q", "position_q", "informal"
)]), ]

baseform <- model.matrix(~ as.factor(gender_q) + as.factor(industry_q) + as.factor(education_q) + as.factor(position_q) - 1,
    data = survey
)

baseform <- baseform[, colnames(baseform) != "as.factor(gender_q)Feminino"]

baseform <- sapply(data.frame(baseform), function(x) {
    x - mean(x)
})

baseform_b <- cbind(baseform, survey)

form <- ""

n <- 0

for (i in colnames(baseform)) {
    n <- n + 1
    f <- paste("treatment * ", i)
    form <- paste(form, f)
    if (n < length(colnames(baseform))) {
        form <- paste(form, " + ")
    }
}

# Q99 salary is fair

form1 <- as.formula(paste("Q99 ~ ", form))

m1 <- lm(Q99 ~ treatment, survey)

m2 <- lm(form1, data = baseform_b)

# Q102 democracy works well

form1 <- as.formula(paste("Q102 ~ ", form))

m3 <- lm(Q102 ~ treatment, survey)

m4 <- lm(form1, data = baseform_b)


m1_se <- get_ses(m1, "HC0")
m2_se <- get_ses(m2, "HC0")
m3_se <- get_ses(m3, "HC0")
m4_se <- get_ses(m4, "HC0")


# table a

starg_iv_labels <- c(
    "Treatment: Lower salary"
)


starg_lines <- list(
    c("Industry \\& Firm Controls", "No", "Yes", "No", "Yes"),
    c("Worker Controls", "No", "Yes", "No", "Yes")
)

cat(stargazer(m1, m2, m3, m4,
    omit = c(
        "Constant",
        "race", "gender_q",
        "industry_q", "education_q", "position_q"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Salary for my work is fair", "Democracy works well"),
    title = "ATE of receiving relatively lower salary information among comparable workers",
    label = "tab:information_treatment",
    se = list(m1_se, m2_se, m3_se, m4_se)
),
file = "tables/qualtrics/information_experiment_brazil.tex", sep = "\n"
)

# PCAs

survey <- read.csv("data/qualtrics/brazil_pca.csv")

# attitudes

m1 <- lm(antisystem ~ compensation_benefits + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m2 <- lm(antisystem ~ autonomy_engagement_fairness + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m3 <- lm(antisystem ~ job_security + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m4 <- lm(antisystem ~ training_opportunities + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m5 <- lm(antisystem ~ work_times + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m6 <- lm(antisystem ~ physical_environment + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m7 <- lm(antisystem ~ compensation_benefits + autonomy_engagement_fairness + job_security + training_opportunities + work_times + physical_environment + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company, data = survey)

m1_se <- get_ses(m1, "HC0")
m2_se <- get_ses(m2, "HC0")
m3_se <- get_ses(m3, "HC0")
m4_se <- get_ses(m4, "HC0")
m5_se <- get_ses(m5, "HC0")
m6_se <- get_ses(m6, "HC0")
m7_se <- get_ses(m7, "HC0")

# table a

starg_iv_labels <- c(
    "Compensation \\& Benefits",
    "Autonomy, Engagement, Fairness",
    "Job security"
)


starg_lines <- list(
    c("Industry \\& Firm Controls", "Yes", "Yes", "Yes"),
    c("Worker Controls", "Yes", "Yes", "Yes")
)

cat(stargazer(m1, m2, m3,
    omit = c(
        "gender_q", "race", "education_q",
        "n_employees", "industry_q", "position_q", "working_time",
        "contract_type_q", "years_in_company"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Anti-System Attitudes (PC 1)"),
    title = "OLS estimates of std. PC of political items on PC of employment items",
    label = "tab:pca_attitudes_a",
    se = list(m1_se, m2_se, m3_se)
),
file = "tables/qualtrics/pca_attitudes_a_brazil.tex", sep = "\n"
)

# table b

starg_iv_labels <- c(
    "Training opportunities",
    "Working times",
    "Physical environment"
)

starg_lines <- list(
    c("Industry \\& Firm Controls", "Yes", "Yes", "Yes"),
    c("Worker Controls", "Yes", "Yes", "Yes")
)

cat(stargazer(m4, m5, m6,
    omit = c(
        "gender_q", "race", "education_q",
        "n_employees", "industry_q", "position_q", "working_time",
        "contract_type_q", "years_in_company"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Anti-System Attitudes (PC 1)"),
    title = "OLS estimates of std. PC of political items on PC of employment items",
    label = "tab:pca_attitudes_b",
    se = list(m4_se, m5_se, m6_se)
),
file = "tables/qualtrics/pca_attitudes_b_brazil.tex", sep = "\n"
)

# table c

starg_iv_labels <- c(
    "Compensation \\& Benefits",
    "Autonomy, Engagement, Fairness",
    "Job security",
    "Training opportunities",
    "Working times",
    "Physical environment"
)

starg_lines <- list(
    c("Industry \\& Firm Controls", "Yes", "Yes", "Yes"),
    c("Worker Controls", "Yes", "Yes", "Yes")
)

cat(stargazer(m7,
    omit = c(
        "gender_q", "race", "education_q",
        "n_employees", "industry_q", "position_q", "working_time",
        "contract_type_q", "years_in_company"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Anti-System Attitudes (PC 1)"),
    title = "OLS estimates of std. PC of political items on PC of employment items",
    label = "tab:pca_attitudes_c",
    se = list(m7_se)
),
file = "tables/qualtrics/pca_attitudes_c_brazil.tex", sep = "\n"
)

# Bolsonaro vote

survey$education_q <- relevel(as.factor(survey$education_q), "Nível inferior ao ensino médio")

survey$race <- relevel(as.factor(survey$race), "Branca")

survey$contract_type_q <- relevel(as.factor(survey$contract_type_q), "Contrato permanente")

survey$position_q <- relevel(as.factor(survey$position_q), "Operário")

survey$industry_q <- relevel(as.factor(survey$industry_q), "Fabricação")

survey$bolsonaro <- ifelse(survey$party_voted == "Jair Bolsonaro (PL)", 1, 0)

survey$lula <- ifelse(survey$party_voted == "Luiz Inácio Lula da Silva (PT)", 1, 0)

m1 <- lm(bolsonaro ~ antisystem + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company + informal, data = survey)

m2 <- lm(bolsonaro ~ behavior + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company + informal, data = survey)

starg_lines <- list(
    c("Industry \\& Firm Controls", "Yes", "Yes", "Yes"),
    c("Worker Controls", "Yes", "Yes", "Yes")
)

starg_iv_labels <- c(
    "Anti-System Attitudes (PC 1)",
    "Anti-System Behaviors (PC 1)"
)

m1_se <- get_ses(m1, "HC0")
m2_se <- get_ses(m2, "HC0")

cat(stargazer(m1, m2,
    omit = c(
        "gender_q", "race", "education_q",
        "n_employees", "industry_q", "position_q", "working_time",
        "contract_type_q", "years_in_company", "informal"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Voted for Bolsonaro in 2022"),
    title = "LPM estimates of std. PC of attitudinal items on Bolsonaro vote",
    label = "tab:bolsonaro_antisystem",
    se = list(m1_se, m2_se)
),
file = "tables/qualtrics/antisystem_bolsonaro.tex", sep = "\n"
)

# Lula supporters

m1 <- lm(lula ~ antisystem + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company + informal, data = survey)

m2 <- lm(lula ~ behavior + gender_q + race + education_q + n_employees + industry_q + position_q + working_time + contract_type_q + years_in_company + informal, data = survey)

starg_lines <- list(
    c("Industry \\& Firm Controls", "Yes", "Yes", "Yes"),
    c("Worker Controls", "Yes", "Yes", "Yes")
)

starg_iv_labels <- c(
    "Anti-System Attitudes (PC 1)",
    "Anti-System Behaviors (PC 1)"
)

m1_se <- get_ses(m1, "HC0")
m2_se <- get_ses(m2, "HC0")

cat(stargazer(m1, m2,
    omit = c(
        "gender_q", "race", "education_q",
        "n_employees", "industry_q", "position_q", "working_time",
        "contract_type_q", "years_in_company", "informal"
    ),
    type = "latex",
    digits = 2,
    omit.stat = c("rsq", "adj.rsq", "f"),
    add.lines = starg_lines,
    covariate.labels = starg_iv_labels,
    dep.var.labels = c("Voted for Lula in 2022"),
    title = "LPM estimates of std. PC of attitudinal items on Lula vote",
    label = "tab:lula_antisystem",
    se = list(m1_se, m2_se)
),
file = "tables/qualtrics/antisystem_lula.tex", sep = "\n"
)